﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Minestorm.Retro.Entities;

namespace Minestorm.Retro.GameLogic
{
    public class EntityArray<T> : EfficientArray<T>
        where T : Entity
    {
        public EntityArray(int initialSize, int growBy)
            : base(initialSize, growBy, e => e.ShouldDraw)
        {
        }

        public void Clear()
        {
            foreach (var t in this)
                t.ShouldDraw = false;

            RemoveAll(t => true);
        }

        public void Add(T entity)
        {
            entity.Index = AddElement(entity);
        }

        public void Remove(T entity)
        {
            RemoveElement(entity.Index);
        }

        public void RemoveAll(Func<T, bool> del)
        {
            var ptr = -1;
            var indices = new int[ArrayLength];

            foreach (var t in this)
                if (del(t))
                    indices[++ptr] = t.Index;

            for (var i = 0; i < ptr; i++)
                RemoveElement(indices[i]);
        }
    }
}
